<template>
  <section>
    <el-row>
      <el-col :span="20">
        <el-form :inline="true" size="mini" ref="form">
          <el-form-item label="选择公司：" >
            <el-select v-model="selCompany" placeholder="请选择" size="mini">
              <el-option v-for="company in companies" :key="company.id" :label="company.companyName" :value="company.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="选择部门：">
            <!--医院列表-选择-->
            <el-select v-model="selDepartment" placeholder="请选择" size="mini">
              <el-option v-for="department in departments" :key="department.id" :label="department.name" :value="department.id"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <el-row>
      <el-button @click="newGoods" size="mini">
        新建商品
      </el-button>
    </el-row>
    <el-row>
      <el-table :data="goods" v-show="goods" size="mini">
        <el-table-column type="index" width="50"></el-table-column>
        <el-table-column label="商品名称">
          <template slot-scope="scope">
            <el-input v-if="scope.row.edit" size="mini" v-model="scope.row.name"></el-input>
            <div v-show="!scope.row.edit">{{scope.row.name}}</div>
          </template>
        </el-table-column>
        <el-table-column prop="goodsType" label="商品类型" >
          <template slot-scope="scope">
            <el-select v-if="scope.row.edit" v-model="scope.row.goodsType.id" placeholder="请选择" size="mini">
              <el-option v-for="goodsType in goodsTypes" :key="goodsType.id" :label="goodsType.name" :value="goodsType.id"></el-option>
            </el-select>
            <div v-if="!scope.row.edit">{{reGoodsTypeName(scope.row)}}</div>
          </template>
        </el-table-column>
        <el-table-column label="规格" width="155">
          <template slot-scope="scope">
            <el-button type='primary' @click='detailBtn(scope)' size="mini" icon="edit">规格管理</el-button>
          </template>
        </el-table-column>
        <el-table-column label="状态" width="155">
          <template slot-scope="scope">
            <el-tooltip v-if="scope.row.state === 0" content="新建商品">
              <el-button type='primary' @click='stateBtn(scope)' size="mini" icon="edit">上架</el-button>
            </el-tooltip>
            <el-button type='primary' @click='stateBtn(scope)' size="mini" icon="edit" v-if="scope.row.state === 2">上架</el-button>
            <el-button type='primary' @click='stateBtn(scope)' size="mini" icon="edit" v-if="scope.row.state === 1">下架</el-button>

          </template>
        </el-table-column>
        <el-table-column label="操作" width="255">
          <template slot-scope="scope">
            <section v-if="scope.row.edit">
              <el-button type="success" @click='confirm(scope)' size="mini" icon="edit">完成</el-button>
              <el-button @click="cancel(scope)" size="mini">取消</el-button>
            </section>
            <section v-else>
              <el-tooltip content="导入到库存">
                <el-button type="primary" size="mini" @click="importToStock(scope)">导入</el-button>
              </el-tooltip>
              <el-button type="primary" @click='confirm(scope)' size="mini" icon="edit" typeof="success">编辑</el-button>
              <el-button @click='del(scope)' size="mini" type="danger">删除</el-button>
            </section>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination v-show="totalPage>0" style="margin-top:10px;"
                     background
                     layout="prev, pager, next"
                     :page-count="totalPage"
                     @current-change="goPage">
      </el-pagination>
    </el-row>

    <el-dialog title="规格管理" :visible.sync="showSpecificationDialog">
      <el-row style="margin-bottom:8px;font-weight: bold">
        <el-col :span="4"><span>商品名称：</span></el-col>
        <el-col :span="8">{{currGoods.name}}</el-col>
        <el-col :span="4"><span>商品类型：</span></el-col>
        <el-col :span="8">{{currGoods.goodsType.name}}</el-col>
      </el-row>
      <el-row style="margin:8px 0;font-weight: bold">
        <el-col :span="4"><span>规格属性：</span></el-col>
        <el-col :span="20">
          <el-tag size="small" style="margin:0 2px;" :key="specification.txt" v-for="specification in specifications" closable :disable-transitions="false" @close="handleClose(specification)">
            {{specification.txt}}
          </el-tag>
          <el-input class="input-new-tag" v-if="inputVisible" v-model="inputValue" ref="saveTagInput" size="mini" @keyup.enter.native="handleInputConfirm" @blur="handleInputConfirm">
          </el-input>
          <el-button v-else v-show="specificationDetail.length==0" class="button-new-tag" size="mini" @click="showInput"> + </el-button>
        </el-col>
      </el-row>
      <el-row>
        <el-table :data="specificationDetail" ref="sepcTable" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55"></el-table-column>
          <!--<el-table-column type="index" width="50"></el-table-column>-->
          <el-table-column v-for="specification in specifications" :key="specification.txt" :label="specification.txt">
            <template slot-scope="scope">
              <el-input v-if="scope.row.edit" size="mini" v-model="specification.data[scope.$index]"></el-input>
              <div v-if="!scope.row.edit">{{specification.data[scope.$index]}}</div>
            </template>
          </el-table-column>
          <el-table-column prop="price" label="价格">
            <template slot-scope="scope">
              <el-input v-if="scope.row.edit" size="mini" v-model="scope.row.price"></el-input>
              <div v-if="!scope.row.edit">{{scope.row.price}}</div>
            </template>
          </el-table-column>

          <el-table-column prop="number" label="商品编号" width="155" >
            <template slot-scope="scope">
              <div v-show="scope.row.edit">
                <el-popover v-show='!scope.row.number' trigger="hover" placement="top-start">
                  <el-button size="mini" type="primary" @click="creatNumbers(scope)">批量生成</el-button>
                  <el-button  slot="reference" @click='creatNumber(scope)' size="mini">
                    生成
                  </el-button>
                </el-popover>
                <div v-show='scope.row.number' size="mini" slot="reference">{{scope.row.number}}</div>
              </div>

              <div v-show="!scope.row.edit">
                <el-popover trigger="hover" placement="top-start">
                  <el-button size="mini" type="primary" @click="copySn(scope.row.number)">复制到所有</el-button>
                  <span size="mini" slot="reference">{{scope.row.number}}</span>
                </el-popover>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="155">
            <template slot-scope="scope">
              <el-button :type="scope.row.edit?'success':'primary'" @click='confirmSpecification(scope)' size="mini" icon="edit">{{scope.row.edit?'完成':'编辑'}}</el-button>
              <el-button @click='delSpecification(scope)' size="mini" >{{scope.row.edit?'取消':'删除'}}</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-row>
      <el-row style="margin-top:10px;">
        <el-button @click="toggleSelection(specificationDetail)" size="mini">反选</el-button>
        <el-button @click="toggleSelection()" size="mini">清除</el-button>
        <span style="margin:0 10px;color:#aaa;">|</span>
        <el-button @click="addSpecifications" size="mini">新建规格</el-button>
        <el-button @click="batchCreateSn" size="mini">生成编号</el-button>
        <!--<el-button @click="save">保存</el-button>-->
      </el-row>
    </el-dialog>
  </section>
</template>

<script>
  import {mapGetters, mapActions, mapMutations} from 'vuex'
  import DateFormat from '@/common/date-formater'
  import schedule from "../../sdk/api/schedule"
  import department from "../../sdk/api/department"
  import Vue from 'vue'//后续添加属性，需要vue联动
  import errHandler from '@/assets/errHandler'

  export default {
    name: "goodsManagement",
    data() {
      return {
        goodsTypes:[],
        selCompany:null,
        companies:[],
        selDepartment:null,
        departments:[],
        goods:null,
        showLoadDialog:false,
        showSpecificationDialog:false,
        scheduleList:null,
        specifications:[],//各种规格
        inputVisible: false,
        inputValue: '',
        specificationDetail:[],
        currGoods:{id:'',name:'',goodsType:{id:'',name:''}},
        skuSelection:[],
        totalPage:0,
        currStockInTypeId:null,
        stockInTypes:null,
        currDepartment:null
      }
    },
    created(){
      //加载公司和部门用于选择
      //加载公司
      this.loadCompanies()
      this.loadGoodsTypes()
      //加载入库类型
      this.getStockInTypes()
    },
    watch:{
      selCompany(nv,ov){
        //根据选择的公司加载部门
        this.loadDepartments(nv)
        this.goods=null
        this.totalPage = 0
      },
      selDepartment(nv,ov){
        //根据选择的部门加载商品
        console.log(nv)
        this.fetch({name:"getDepartmentById",data:{id:nv}})
          .then(res=>{
            console.log(res)
            this.currDepartment = res
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
        this.loadGoods(nv)
      },
      currStockInTypeId(nv,ov){
        if(nv){
          let data = {stockInTypeId:nv,goodsSkuId:'',totalStock:20}
        }
      }
    },
    methods: {
      ...mapActions({
        fetch: 'fetch'
      }),
      getStockInTypes(){
        this.fetch({name:"getStockInTypes"})
          .then(res=>{
            console.log(res)
            this.stockInTypes = res
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      test(scope,res){
        console.log(scope)
        for(let i=0;i<scope.row.goodsSkus.length;i++){
          let tmp = scope.row.goodsSkus[i]
          console.log(tmp)
          let skuArr = tmp.props.split(';')
          let props=''
          if(skuArr.length >0){
            skuArr.forEach(sku=>{
              let kv = sku.split(':')
              if(kv.length === 2){
                props +=kv[1]
              }
            })
          }
          let newP = props.replace(/-/g,'')
          console.log(scope.row.name+'-'+newP)
          let dataItem = {
            stockInTypeId:res.stockInType.id,
            godownEntryId:res.id,
            num:20,
            unit:'',sn:tmp.sn,title:scope.row.name+'-'+newP}
          dataItem.data={
            props:newP?newP:'',
            date:tmp.data?tmp.data.date:'',
            goodsName:scope.row.name
          }
          console.log(dataItem)
          this.createGodownEntryItem(dataItem)
        }
      },
      importToStock(scope){//导入到入库单 'stockInTypeId','title'
        console.log(scope)
        let title = scope.row.name
        if(scope.row.import && scope.row.import === true){//判断是否导入
          console.log('已导入')
        }else {//跳出编辑对话框
          let stockInTypeCode ="production_stock_in"
          if(stockInTypeCode){
            let params={stockInTypeCode,title,departmentCode:this.currDepartment.code,warehouseCode:'default_warehouse'}
            params.data={
              departmentName:this.currDepartment.name,
              companyId:this.currDepartment.companyId,
              companyName:this.currDepartment.companyName,
              goodsName:title
            }
            console.log(params)
            this.fetch({name:"createGodownEntry",data:params})
              .then(res=>{
                this.test(scope,res)
                this.$message({type:'success',message:'入库单生成成功！'})
              })
              .catch(err=>{
                console.log(err)
                errHandler.handle(this,err)
              })
          }
        }
      },
      createGodownEntryItem(data){
        console.log(data)
        this.fetch({name:"createGodownEntryItem",data:data})
          .then(res=>{
            this.$message({type:'success',message:'入库单项导入成功'})
            console.log(res)
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      judgStockInType(code){
        return "production_stock_in"
      },
      goPage(val){
        this.loadGoods(this.selDepartment,val-1)
      },
      cancel(scope){
        scope.row.edit=false
      },
      handleClose(tag) {
        this.specifications.splice(this.specifications.indexOf(tag), 1)
      },
      handleSelectionChange(vals){
        this.skuSelection = vals
      },
      toggleSelection(rows) {
        if (rows) {
          rows.forEach(row => {
            this.$refs.sepcTable.toggleRowSelection(row);
          });
        } else {
          this.$refs.sepcTable.clearSelection();
        }
      },
      createSn(sku){
        console.log(sku)
        this.fetch({name:"createSn",data:{snType:4}})
          .then(res=>{
            console.log(res)
            sku.number = res.sn
            //判断更新还是新建
            //查找当前sku的index
            let index = this.specificationDetail.findIndex(sd=>sd===sku)
            // this.makeFormData(scope)
            //组装sku等data
            let skus=''
            for(let i=0;i<this.specifications.length;i++){
              let tmp = this.specifications[i]
              if(!tmp.data[index])return null//如果没有编辑过，就没有，不生成
              skus +=tmp.txt+':'+tmp.data[index]+';'
            }
            if(skus.length>0)
              skus = skus.substring(0,skus.length-1)
            let data = {goodsId:this.currGoods.id,props:skus,sn:sku.number,price:sku.price}
            console.log(data)
            if(sku.id){
              data.id=sku.id
              this.updateGoodsSku(sku,data)
            } else
              this.createGoodsSku(sku,data)
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      batchCreateSn(){
        if(this.skuSelection.length===0){
          this.$message({type: 'error', message: '没有选中任何规格'})
          return
        }
        this.skuSelection.forEach(sku=>{
          this.createSn(sku)
        })
      },
      showInput() {
        this.inputVisible = true
        this.$nextTick(_ => {
          this.$refs.saveTagInput.$refs.input.focus()
        });
      },
      handleInputConfirm() {
        let inputValue = this.inputValue
        if (inputValue) {
          this.specifications.push({txt:inputValue,data:[]})
        }
        this.inputVisible = false
        this.inputValue = ''
      },
      formatDate(row, column, cellValue, index){
        return DateFormat.format(cellValue)
      },
      reGoodsTypeName(row){//计算商品类型名字，用于显示
        if(row.goodsTypeId){
          let tmpH = this.goodsTypes.find(i =>i.id === row.goodsTypeId)
          if(tmpH)
            return tmpH.name
          else
            return '没找到'
        }else {
          return row.goodsType.name
        }
      },
      loadGoodsTypes(){//加载商品类型列表
        this.fetch({name:"getGoodsTypeList"})
          .then(res=>{
            this.goodsTypes = res
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      loadCompanies(){//加载公司列表
        this.fetch({name:"getCompanyList"})
          .then(res=>{
            this.companies = res
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      loadDepartments(companyId){//加载部门列表
        this.fetch({name:"getDepartmentList",data:{companyId}})
          .then(res=>{
            this.departments=res
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      loadGoods(departmentId,pageNo){//加载商品列表
        this.fetch({name:"getGoodsList",data:{departmentId,page:pageNo? pageNo:0}})
          .then(res=>{
            console.log(res)
            this.goods=res.content
            this.totalPage = res.totalPages
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      newGoods(){//新建商品
        console.log(this.goods)
        if(this.goods){//已经选完部门了
          let newGoods = {edit:true,name:'',goodsType:{}}
          // this.goods.push(newGoods)
          this.goods.unshift(newGoods)
        }

      },
      loadScheduleList(departmentId){//加载排班列表
        console.log(departmentId)
        this.fetch({name:"getScheduleList",data:{departmentId,page:'0'}})
          .then(res=>{
            this.scheduleList=res.content
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      confirm(scope){
        console.log(scope)
        if(scope.row.edit && scope.row.edit === true){//目前是编辑状态
          let data={goodsTypeId:scope.row.goodsType.id,name:scope.row.name,departmentId:this.selDepartment}
          //判断是否新创建
          if(scope.row.id){//更新
            console.log('更新')
            data.id = scope.row.id
            console.log(data)
            this.updateGoods(data)
          }
          else {//新建
            console.log('新建')
            this.createGoods(data)
          }
        }else {
          if(scope.row.edit === undefined){
            Vue.set(scope.row,'edit',true)//后续添加属性，需要vue联动
          }else {
            scope.row.edit = true
          }
        }
      },
      createGoods(data){
        console.log(data)
        this.fetch({name:"createGoods",data:data})
          .then(res=>{
            this.currGoods=res
            this.loadGoods(data.departmentId)
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      updateGoods(data){//更新商品
        console.log(data)
        this.fetch({name:"updateGoods",data:data})
          .then(res=>{
            console.log(res)
            this.loadGoods(res.departmentId)
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      del(scope){//删除商品
        console.log(scope)
        this.fetch({name:"deleteGoods",data:{id:scope.row.id}})
          .then(res=>{
            this.goods.splice(scope.$index,1)
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      getGoodsSkus(res){
        console.log(res)
        this.specifications=[]
        this.specificationDetail = []
        if(res && res.length>0){
          for(let i=0;i<res.length;i++){
            this.specificationDetail.push({id:res[i].id,price:res[i].price,number:res[i].sn,edit:false})
            //还有一部分数据
            let tmpskuArr = res[i].props.split(';')
            if(tmpskuArr.length>0){//properties
              tmpskuArr.forEach(sku=>{
                let kv = sku.split(':')
                if(kv.length === 2){
                  let spec = this.specifications.find(s=> s.txt===kv[0])//key 相等
                  if(!spec){
                    //规格 项新建
                    spec = {txt:kv[0],data:[]}
                    this.specifications.push(spec)
                  }
                  spec.data[i]=kv[1]
                }
              })
            }
          }
        }
        console.log(this.specificationDetail)
        console.log(this.specifications)
        // this.fetch({name:"getGoodsSkuList",data:{goodsId:this.currGoods.id}})
        //   .then(res=>{
        //     this.specifications=[]
        //     this.specificationDetail = []
        //     for(let i=0;i<res.length;i++){
        //       this.specificationDetail.push({id:res[i].id,price:res[i].price,number:res[i].sn,edit:false})
        //       //还有一部分数据
        //       let tmpskuArr = res[i].props.split(';')
        //       if(tmpskuArr.length>0){//properties
        //         tmpskuArr.forEach(sku=>{
        //           let kv = sku.split(':')
        //           if(kv.length === 2){
        //             let spec = this.specifications.find(s=> s.txt===kv[0])//key 相等
        //             if(!spec){
        //               //规格 项新建
        //               spec = {txt:kv[0],data:[]}
        //               this.specifications.push(spec)
        //             }
        //             spec.data[i]=kv[1]
        //           }
        //         })
        //       }
        //     }
        //   })
        //   .catch(err=>{
        //     console.log(err)
        //     errHandler.handle(this,err)
        //   })
      },
      copySn(sn){
        this.specificationDetail.forEach(s=>{
          if(s.number !==sn){
            s.number=sn
            s.edit = true
          }
        })
      },
      // updateGoods(row,state){
      //   let data = {id:row.id,state}
      //   console.log(data)
      //   this.fetch({name:"updateGoods",data:{id:row.id,state}})
      //     .then(res=>{
      //       console.log(res)
      //       if(res.state === 2){
      //         row.state = 2
      //         this.$message({
      //           type: 'success',
      //           message: '下架成功!'
      //         })
      //       }else {
      //         row.state = 1
      //         this.$message({
      //           type: 'success',
      //           message: '上架成功!'
      //         })
      //       }
      //     })
      //     .catch(err=>{
      //       console.log(err)
      //       errHandler.handle(this,err)
      //     })
      // },
      stateBtn(scope){
        console.log(scope)
        if(scope.row.state && scope.row.state === 1){//在架上
          console.log('下架')
          this.$confirm('此操作将下架商品，用户端无法显示?', '下架', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            let data={id:scope.row.id,state:2}
            this.updateGoods(data)
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消下架'
            })
          })
        }else {//0和2表示不在架上
          console.log('上架')
          this.$confirm('确定上架?', '上架', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            let data={id:scope.row.id,state:1}
            this.updateGoods(data)
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消上架'
            })
          })
        }

      },
      detailBtn(scope){//进入规格管理,加载已有规格
        this.currGoods = this.goods[scope.$index]//定位当前商品
        console.log(this.currGoods)
        this.getGoodsSkus(this.currGoods.goodsSkus)
        this.showSpecificationDialog = true
      },
      addSpecifications(){//增加规格
        let tmp = {price:'0',number:'',edit:true}
        this.specificationDetail.push(tmp)
        //新建规格
      },
      makeFormData(scope){
        console.log(scope)
        console.log(this.specifications)
        //组装sku等data
        let sku=''
        for(let i=0;i<this.specifications.length;i++){
          let tmp = this.specifications[i]
          sku +=tmp.txt+':'+tmp.data[scope.$index]+';'
        }
        if(sku.length>0)
          sku = sku.substring(0,sku.length-1)
        console.log(sku)
        let data = {goodsId:this.currGoods.id,props:sku,sn:scope.row.number,price:scope.row.price}
        return data
      },
      confirmSpecification(scope){//确定规格按钮判断编辑还是新建
        console.log(this.specificationDetail)
        console.log(scope)
        if(scope.row.edit && scope.row.edit === true){//目前是编辑状态
          //组装sku等data
          let data = this.makeFormData(scope)
          console.log(data)
          //判断是否新创建
          if(scope.row.id){//非新建，有id，要更新
            console.log('要更新')
            data.id=scope.row.id
            this.updateGoodsSku(scope.row,data)
          }
          else {//新建的，有edit//保存新
            this.createGoodsSku(scope.row,data)
          }
        }else {
          console.log('点之前非编辑，改成编辑')
          //点击之前是非编辑状态，改成编辑状态
          if(scope.row.edit === undefined){
            Vue.set(scope.row,'edit',true)//后续添加属性，需要vue联动
          }else {
            scope.row.edit = true
          }
        }
      },
      delSpecification(scope){
        console.log(scope)
        if(scope.row.edit){//如果是true，就是取消
          if(scope.row.id){
            scope.row.edit = false
          }else {
            this.specificationDetail.splice(scope.$index,1)
          }
        }else {//如果是false，就是删除
          if(scope.row.id){
            this.fetch({name:"deleteGoodsSku",data:{id:scope.row.id}})
              .then(res=>{
                for(let i=0;i<this.goods.length;i++){
                  let g = this.goods[i]
                  if(g.goodsSkus && g.goodsSkus.length>0){
                    let index = g.goodsSkus.findIndex(s => s.id === scope.row.id)
                    if(index>=0){
                      console.log(scope.row,g.goodsSkus[index])
                      g.goodsSkus.splice(index,1)
                      this.specificationDetail.splice(index,1)
                      // console.log(g.goodsSkus)
                      // this.getGoodsSkus(g.goodsSkus)
                    }
                  }else{
                    console.log('不存在skus')
                  }
                }
              })
              .catch(err=>{
                console.log(err)
                errHandler.handle(this,err)
              })
          }else {
            console.log('没有id号')
          }
        }
      },
      creatNumber(scope){
        console.log(scope)
        this.fetch({name:"createSn",data:{snType:4}})
          .then(res=>{
            console.log(res)
            scope.row.number = res.sn
            //判断更新还是新建

          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      creatNumbers(scope){//批量生成后需要马上保存……
        console.log(scope)
        let tmp
        let s={$index:'',row:''}
        for(let i=scope.$index;i<this.specificationDetail.length;i++){
          console.log('进入循环')
          tmp = this.specificationDetail[i]
          if(tmp.number === ''){
            let sIndex = this.specifications.findIndex(s => s.data[i])
            if(sIndex < 0){
              console.log('规格未填完')
            }else {
              s.$index = i
              s.row = tmp
              this.creatNumber(s)
            }
          }
        }
      },
      createGoodsSku(row,data){//创建商品规格
        console.log(row)
        this.fetch({name:"createGoodsSku",data:data})
          .then(res=>{
            row.id=res.id
            console.log(res)
            this.partialUpdate(data.goodsId,res)
            row.edit=false
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      partialUpdate(goodsId,res){//局部更新
        let g = this.goods.find(gs=>gs.id === goodsId)
        if(g){
          if(g.goodsSkus && g.goodsSkus.length>0){
            let index = g.goodsSkus.findIndex(s => s.id === res.id)
            if(index>=0){
              g.goodsSkus[index]=res
            }else {
              g.goodsSkus.push(res)
            }
          }else{
            console.log(g)
            console.log('不存在skus')
          }
        }
      },
      updateGoodsSku(row,data){
        this.fetch({name:"updateGoodsSku",data:data})
          .then(res=>{
            this.partialUpdate(data.goodsId,res)
            row.edit=false
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
    }
  }
</script>

<style lang="scss">

  .sn-box{position: relative;
    .sn-tool-box{display: none}
    &:hover{
      .sn-tool-box{display: block}
    }
  }
</style>
